package com.cn.controller;

import com.cn.baseModel.DataResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class ExceptionController {

    /**
     * 统一处理所有异常
     * @param request
     * @param ex
     * @return 异常信息
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public DataResponse globalException(HttpServletRequest request,Throwable ex){
        ex.printStackTrace();
        return DataResponse.error(ex.getMessage(),"-1");
    }
}
